﻿using Inaction.Rule;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Inaction.Service.Client
{
    public partial class AppConfigForm : Form
    {
        public AppConfigForm()
        {
            InitializeComponent();

            serviceTypeBindingSource.DataSource = EnumDescriptionList.Query(typeof(ServiceType));
            serializerTypeBindingSource.DataSource = EnumDescriptionList.Query(typeof(SerializeType));
            sqlTypeBindingSource.DataSource = EnumDescriptionList.Query(typeof(SqlType));

            appConfigBindingSource.DataSource = Inaction.AppConfig.Instance;
            flowLayoutPanel_DbConfig.Visible = false;
            flowLayoutPanel_ServiceConfig.Visible = false;
        }

        private void comboBox_ServerType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox_ServerType.SelectedValue == null) return;
            var type = (ServiceType)comboBox_ServerType.SelectedValue;
            if (type != ServiceType.Local)
            {
                flowLayoutPanel_DbConfig.Visible = false;
                flowLayoutPanel_ServiceConfig.Visible = true;
                this.Size = new Size(266, 193);
            }
            else
            {
                flowLayoutPanel_DbConfig.Visible = true;
                flowLayoutPanel_ServiceConfig.Visible = false;
                this.Size = new Size(266, 266);
            }
            btn_Save.Location = new Point(btn_Save.Location.X, this.ClientSize.Height - 5 - btn_Save.Size.Height);
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            appConfigBindingSource.EndEdit();
            Inaction.AppConfig.Instance.AppType = AppType.WinForm;
            if (Inaction.AppConfig.Instance.ServiceType == ServiceType.Local)
            {
                Inaction.AppConfig.Instance.RunLocation = RunLocation.Local;
            }
            else
            {
                Inaction.AppConfig.Instance.RunLocation = RunLocation.Client;
            }
            Inaction.AppConfig.Instance.Save();
            MessageBox.Show("保存成功!");
            Close();
        }
    }
}
